package com.yiren.dbaa.module.project_edit.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.currency.utils.FileUtil;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.site.bean.ExamMerge;
import com.yiren.dbaa.module.practice_set.PracticeCarSetActivity;
import com.yiren.dbaa.module.project_edit.bean.ExpandableBean;
import com.yiren.dbaa.module.project_edit.service.IDataOperation;
import com.yiren.dbaa.module.project_edit.service.impl.DataOperationImpl;
import com.yiren.dbaa.module.project_edit.view.adapter.ExpandableAdapter;
import com.yiren.dbaa.module.project_edit.view.fragment.JjdFragment;
import com.yiren.dbaa.module.project_edit.view.fragment.LightParamFragment;
import com.yiren.dbaa.module.project_edit.view.fragment.RuleEditFragment;
import com.yiren.dbaa.module.project_edit.view.fragment.SczbFragment;
import com.yiren.dbaa.module.project_edit.view.fragment.SiteInfoSetFragment;
import com.yiren.dbaa.module.project_edit.view.fragment.VoiceSetFragment;
import com.yiren.dbaa.module.project_edit.view.fragment.GroupLight_Fragment;
import com.yiren.dbaa.module.project_edit.view.fragment.SingleLight_Fragment;
import com.yiren.dbaa.view.dialog.LoadDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/10/31 10:25
 * @description: 编辑规则首页
 */
public class ProjectManageActivity extends BaseActivity {

    @BindView(R.id.lv_project_title)
    ExpandableListView lvProjectTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    private ExpandableAdapter adapter;
    private List<ExpandableBean> expandableBeanList;

    private SiteInfoSetFragment siteInfoSetFragment;
    private VoiceSetFragment voiceSetFragment;
    private GroupLight_Fragment groupLightFragment;
    private SingleLight_Fragment singleLightFragment;
    private LightParamFragment lightParamFragment;
    private SczbFragment sczbFragment;
    private JjdFragment jjdFragment;
    private RuleEditFragment ruleEditFragment;

    private Bundle bundle;
    private SiteEntity site;
    private Gson gson = new Gson();

    private IDataOperation iDataOperation;
    private List<ProjectEntity> projectList;//项目集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_manage);
        ButterKnife.bind(this);
        tvTitle.setText("规则编辑");
        bundle = new Bundle();
        site = (SiteEntity) getIntent().getSerializableExtra("site");
        bundle.putSerializable("site", site);
        //合并数据
        ExamMerge examMerge = new ExamMerge(this);
        LoadDialog.show(this, "正在加载数据...");
        examMerge.loadExamData(site, new CommonCallBack() {
            @Override
            public void success(Object result) {
                runOnUiThread(() -> {
                    LoadDialog.dismiss(ProjectManageActivity.this);
                    init();
                    selectTab(0, 0);
                });
            }
        });
    }

    private void init() {
        iDataOperation = new DataOperationImpl();
        projectList = iDataOperation.getProjectList();
        String expandableModeStr = FileUtil.readAssetsFile("ExpandableMode.json", this);
        expandableBeanList = gson.fromJson(expandableModeStr, new TypeToken<List<ExpandableBean>>() {
        }.getType());
        for (int i = 0; i < projectList.size(); i++) {
            ExpandableBean.ChildDataBean childDataBean = new ExpandableBean.ChildDataBean();
            childDataBean.setChildName(projectList.get(i).getName());
            expandableBeanList.get(2).getChildData().add(childDataBean);
        }
        adapter = new ExpandableAdapter(expandableBeanList, this);
        lvProjectTitle.setAdapter(adapter);
        // 打开所有
        for (int i = 0; i < expandableBeanList.size(); i++) lvProjectTitle.expandGroup(i);
        lvProjectTitle.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                adapter.refreshAdapter(groupPosition, childPosition);
                selectTab(groupPosition, childPosition);
                return false;
            }
        });

    }

    /**
     * 进行选中Tab的处理
     */
    private void selectTab(int groupPosition, int childPosition) {
        //获取FragmentManager对象
        FragmentManager manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (groupPosition) {
            //当选中一级时
            case 0:
                //当选中二级时
                switch (childPosition) {
                    case 0:
                        if (siteInfoSetFragment == null) {
                            siteInfoSetFragment = new SiteInfoSetFragment();
                            siteInfoSetFragment.setArguments(bundle);
                            transaction.add(R.id.fl_content, siteInfoSetFragment);
                        } else {
                            transaction.show(siteInfoSetFragment);
                        }
                        break;
                    case 1:
                        if (voiceSetFragment == null) {
                            voiceSetFragment = new VoiceSetFragment();
                            voiceSetFragment.setArguments(bundle);
                            transaction.add(R.id.fl_content, voiceSetFragment);
                        } else {
                            transaction.show(voiceSetFragment);
                        }
                        break;
                }
                break;
            case 1:
                switch (childPosition) {
                    case 0:
                        if (groupLightFragment == null) {
                            groupLightFragment = new GroupLight_Fragment();
                            groupLightFragment.setArguments(bundle);
                            transaction.add(R.id.fl_content, groupLightFragment);
                        } else {
                            transaction.show(groupLightFragment);
                        }
                        break;
                    case 1:
                        if (singleLightFragment == null) {
                            singleLightFragment = new SingleLight_Fragment();
                            singleLightFragment.setArguments(bundle);
                            transaction.add(R.id.fl_content, singleLightFragment);
                        } else {
                            transaction.show(singleLightFragment);
                        }
                        break;
                    case 2:
                        if (lightParamFragment == null) {
                            lightParamFragment = new LightParamFragment();
                            lightParamFragment.setArguments(bundle);
                            transaction.add(R.id.fl_content, lightParamFragment);
                        } else {
                            transaction.show(lightParamFragment);
                        }
                        break;
                }
                break;
            case 2:
                switch (childPosition) {
                    case 0:
                        if (sczbFragment == null) {
                            sczbFragment = new SczbFragment();
                            sczbFragment.setArguments(bundle);
                            transaction.add(R.id.fl_content, sczbFragment);
                        } else {
                            transaction.show(sczbFragment);
                        }
                        break;
                    case 1:
                        if (jjdFragment == null) {
                            jjdFragment = new JjdFragment();
                            jjdFragment.setArguments(bundle);
                            transaction.add(R.id.fl_content, jjdFragment);
                        } else {
                            transaction.show(jjdFragment);
                        }
                        break;
                    default:
                        ruleEditFragment = null;
                        bundle.putInt("child", childPosition - 2);
                        ruleEditFragment = new RuleEditFragment();
                        ruleEditFragment.setArguments(bundle);
                        transaction.add(R.id.fl_content, ruleEditFragment);

//                        if (ruleEditFragment == null) {
//                            ruleEditFragment = new RuleEditFragment();
//                            ruleEditFragment.setArguments(bundle);
//                            transaction.add(R.id.fl_content, ruleEditFragment);
//                        } else {
//                            transaction.show(ruleEditFragment);
//                            ruleEditFragment.loadRule(childPosition - 2);
//                        }
                        break;
                }
                break;
        }
        transaction.commit();
    }

    //将Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (siteInfoSetFragment != null) {
            transaction.hide(siteInfoSetFragment);
        }
        if (voiceSetFragment != null) {
            transaction.hide(voiceSetFragment);
        }
        if (groupLightFragment != null) {
            transaction.hide(groupLightFragment);
        }
        if (singleLightFragment != null) {
            transaction.hide(singleLightFragment);
        }
        if (lightParamFragment != null) {
            transaction.hide(lightParamFragment);
        }
        if (sczbFragment != null) {
            transaction.hide(sczbFragment);
        }
        if (jjdFragment != null) {
            transaction.hide(jjdFragment);
        }
        if (ruleEditFragment != null) {
            transaction.hide(ruleEditFragment);
        }
    }

    @OnClick({R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                startActivity(new Intent(this, PracticeCarSetActivity.class));
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            startActivity(new Intent(this, PracticeCarSetActivity.class));
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

}